package com.woniuxy.tfss.web.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.tfss.entity.Discount;
import com.woniuxy.tfss.entity.ResponseEntity;
import com.woniuxy.tfss.service.DiscountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Controller("adminDiscountController")
@RequestMapping("admin/discount")
@CrossOrigin
@Slf4j
public class DiscountController {
    @Autowired
    private DiscountService discountService;

    /**
     * 查询所有优惠券
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity<List<Discount>> list(){
        List<Discount> discounts=discountService.list();
        discounts.forEach(d->System.out.println(d.getDiscount()));
        return new ResponseEntity<>(discounts);
    }

    /**
     * 增加优惠券
     * @param discount
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResponseEntity<Void> add(@RequestBody Discount discount){
        discountService.save(discount);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 修改优惠券信息
     * @param discount
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public ResponseEntity<Void> update(@RequestBody Discount discount){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",discount.getId());
       discountService.update(discount,wrapper);
       return ResponseEntity.SUCCESS;
    }
}
